const {
  Service,
} = require('uni-cloud-router')
const {
  TaskType,
} = require('../constants/const')

const {
  generateToken,
} = require('../utils/Auth')
const { getDate } = require('../utils/Date')
const ResponseUtil = require('../utils/Response')

module.exports = class UserService extends Service {
  async info(data) {
    const { openId } = data

    const user = this.db.collection('user')
    const findUser = await user.where({
      openid: openId,
    }).get()

    const { avatarUrl, nickName } = findUser.data[0]

    return ResponseUtil.success({
      avatar: findUser.data[0].avatarUrl,
      userName: findUser.data[0].nickName,
      createdAt: findUser.data[0].createdAt,
    })
  }

  async update(data) {
    const { openId, avatarUrl, nickName } = data

    const user = this.db.collection('user')

    const count = await user.where({
      openid: openId,
    }).count()

    if (count.total > 0) {
      user.where({
        openid: openId,
      }).update({
        avatarUrl,
        nickName,
      })
    }

    return ResponseUtil.success(null, '成功')
  }
}
